package com.homewoek.thread;

/**
 * @Author:Ren2048
 * @Date:2021/9/1
 * @Description:com.homewoek.thread
 * @Version:1.8.0_261
 */

//2.多线程实现业务流程：员工张三有两个领导，
// 领导A和领导B经常会根据张三的表现给他调整工资，有可能增加也有可能减少。
// 用两个线程来执行领导A和领导B给张三调工资的工作，使用多线程同步来实现，解决数据完整性问题。
// 保证最终工资真实可靠。

public class SynSalary {
    static class People{
        private double salary = 10000.0d;

        synchronized public void synIncrease(double money){
            System.out.println("salary = " + salary + "\tincrease: " + money);
            salary += money;
            System.out.println("now salary = " + salary);
        }

        synchronized public void synPayCut(double money){
            System.out.println("salary = " + salary + "\tPayCut: " + money);
            salary -= money;
            System.out.println("now salary = " + salary);
        }
    }

    static class PeopleThread extends Thread{
        private People people = null;
        private String method = "";

        public PeopleThread(People people, String method){
            this.people = people;
            this.method = method;
        }

        @Override
        public void run() {
            if (method.equals("synIncrease"))
                people.synIncrease(400);
            else if (method.equals("synPayCut"))
                people.synPayCut(300);
        }
    }

    public static void main(String[] args) {
        People people = new People();
        Thread threadA = new PeopleThread(people,"synIncrease");
        Thread threadB = new PeopleThread(people,"synPayCut");
        threadA.start();
        threadB.start();
    }
}
